package cn.com.joyplus.wechat.model;

import cn.com.joyplus.wechat.configuration.CacheConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 王灏川(wangrs@qq.com) on 2017/8/11 0011.
 */
@Component
public class WxMsgCache {
    private static final Logger logger = LoggerFactory.getLogger(WxMsgCache.class);

    private List<String> historyMsgIds = new ArrayList<>();

    @CachePut(value = CacheConfig.CACHE_FOR_WXMSG)
    public void putMsgIdToCache(String msgId) {
        logger.info(String.format("Put MsgId %s To Cache.", msgId));
        historyMsgIds.add(msgId);
    }

    @Cacheable(CacheConfig.CACHE_FOR_WXMSG)
    public String getMsgIdFromCache(String msgId) {
        logger.info(String.format("Get MsgId %s From Cache.", msgId));
        if (historyMsgIds.contains(msgId)) return msgId;
        return null;
    }

    @CacheEvict(CacheConfig.CACHE_FOR_WXMSG)
    public void removeMsgIdFromCache(String msgId) {
        logger.info(String.format("Remove MsgId %s From Cache.", msgId));
        historyMsgIds.remove(msgId);
    }
}
